package com.tanhua.server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.tanhua.model.domain.Answer;
import com.tanhua.model.domain.Mock;
import com.tanhua.model.dto.AnswersDto;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.ReportVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.service.TestSoulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/testSoul")
public class TestSoulController {
    @Autowired
    private TestSoulService testSoulService;
    /**
     * 问卷列表
     * 接收参数
     */
    @GetMapping
    public ResponseEntity getMockList(){
        //调用service
        List<Mock> mocks=testSoulService.getMockList();
        return ResponseEntity.ok(mocks);
    }

    /**
     * 提交问卷并进行相应的分析和存储
     * 注意前端传的是个对象不是集合,所以想用一个对象去接参数
     * @param answers
     * @return
     */
    @PostMapping
    public ResponseEntity submit(@RequestBody AnswersDto answers){
        //参数判断
        if (answers==null) {
            throw new BusinessException(ErrorResult.builder().errMessage("参数异常").build());
        }
        List<Answer> answers1 = answers.getAnswers();
        if (answers1==null||answers1.size()==0){
            throw new BusinessException(ErrorResult.builder().errMessage("参数异常").build());
        }
        //返回报告id
        String reportId=testSoulService.submit(answers1);
        return ResponseEntity.ok(reportId);
    }

    /**
     * 返回报告
     * @param reportId
     * @return
     */
    @GetMapping("/report/{reportId}")
    public ResponseEntity getReport(@PathVariable String reportId){
        //首先进行参数判断
        //参数判断
        if (reportId==null||"".equals(reportId)) {
            throw new BusinessException(ErrorResult.builder().errMessage("参数异常").build());
        }
        //调service层
        ReportVo reportVo=testSoulService.getReport(reportId);
        return ResponseEntity.ok(reportVo);
    }



}
